//
//  ExhibitorsDetailsHeadCell.m
//  ppkc2016
//
//  Created by 拼凑的断音 on 16/3/29.
//  Copyright © 2016年 拼凑的断音. All rights reserved.
//

#import "ExhibitorsDetailsHeadCell.h"
#import "XXTool.h"
#import "DKPhotoBrowser.h"

@interface ExhibitorsDetailsHeadCell ()<UIScrollViewDelegate,DKPhotoBrowserDelegate>

@property (nonatomic) UIScrollView* backgroundScrollImg;

@property (nonatomic) UIPageControl* pagecontrol;

@property (nonatomic) VerticalSettingLabel* exhibitorName;

@property (nonatomic) DKButton *collectButton;

@property (nonatomic) DKButton *bespeakButton;

@property (nonatomic) NSMutableArray *saveImageArray;

@end

@implementation ExhibitorsDetailsHeadCell
{
    NSInteger pageNumber;
    NSArray *imgUrlArray;
    NSMutableArray *imgNeedShowBigArray;
    NSMutableArray *nameLabelArray;
}
-(UIScrollView*)backgroundScrollImg
{
    if (_backgroundScrollImg==nil)
    {
        _backgroundScrollImg = [[UIScrollView alloc] init];
        _backgroundScrollImg.pagingEnabled=YES;//立刻翻页到下一页 没中间的拖动过程
        _backgroundScrollImg.bounces=NO;//去掉翻页中的白屏
        _backgroundScrollImg.showsHorizontalScrollIndicator=NO;//不现实水平滚动条
        _backgroundScrollImg.userInteractionEnabled = YES;
        [_backgroundScrollImg setDelegate:self];
        [self addSubview:_backgroundScrollImg];
    }
    return _backgroundScrollImg;
}

-(UIPageControl*)pagecontrol
{
    if (_pagecontrol==nil)
    {
        _pagecontrol = [[UIPageControl alloc] init];
        _pagecontrol.numberOfPages=0;
        _pagecontrol.currentPage=0;
        [_pagecontrol setCurrentPageIndicatorTintColor:[UIColor whiteColor]];
        [_pagecontrol setPageIndicatorTintColor:[UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.5]];
        [self addSubview:_pagecontrol];
    }
    return _pagecontrol;
}

-(DKButton*)collectButton
{
    if (_collectButton==nil)
    {
        _collectButton = [[DKButton alloc] init];
        _collectButton.image = [UIImage imageNamed:@"收藏按钮_未点击"];
        _collectButton.text = NSLocalizedString(@"收藏", nil);
        _collectButton.imagePosition = DKButtonImagePositionAtBegan;
        [_collectButton setColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7] ForState:DKButtonStateNormal];
        _collectButton.font = [XXTool systemFontOfSize:14];
        _collectButton.imageSize = CGSizeMake(20, 20);
        _collectButton.imageTitleGap = 23.0f;
        [_collectButton setImageRenderingMode:DKButtonRenderingModeAlwaysOriginal];
        [_collectButton addTarget:self Select:@selector(clickCollectButton) ForEvent:DKButtonEventPressedEnd];
        [self addSubview:_collectButton];
        
        if ([UserInfo shareUserInfo].isExhibitor||[[UserInfo shareUserInfo].userID isEqualToString:@""])
        {
            _collectButton.hidden = YES;
        }
    }
    return _collectButton;
}

-(DKButton*)bespeakButton
{
    if (_bespeakButton==nil)
    {
        _bespeakButton = [[DKButton alloc] init];
        _bespeakButton.image = [UIImage imageNamed:@"预约按钮_未点击"];
        _bespeakButton.text = NSLocalizedString(@"预约", nil);
        _bespeakButton.imagePosition = DKButtonImagePositionAtBegan;
        [_bespeakButton setColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:0.7] ForState:DKButtonStateNormal];
        _bespeakButton.font = [XXTool systemFontOfSize:14];
        _bespeakButton.imageSize = CGSizeMake(20, 20);
        _bespeakButton.imageTitleGap = 23.0f;
        [_bespeakButton addTarget:self Select:@selector(clickBespeakButton) ForEvent:DKButtonEventPressedEnd];
        [_bespeakButton setImageRenderingMode:DKButtonRenderingModeAlwaysOriginal];
        [self addSubview:_bespeakButton];
        
        if ([UserInfo shareUserInfo].isExhibitor||[[UserInfo shareUserInfo].userID isEqualToString:@""])
        {
            _bespeakButton.hidden = YES;
        }
    }
    return _bespeakButton;
}

-(void)initUI
{
    
}

-(void)refreshLayout:(CGRect)frame
{
    _backgroundScrollImg.frame = CGRectMake(0, 0, frame.size.width
                                       , frame.size.height);
    
    
    
    self.collectButton.frame = CGRectMake(0, 130
                                          , (frame.size.width-4)/2
                                          , frame.size.height-130);
    self.bespeakButton.frame = CGRectMake(frame.size.width-_collectButton.frame.size.width
                                          , _collectButton.frame.origin.y
                                          , _collectButton.frame.size.width
                                          , _collectButton.frame.size.height);
    
    _pagecontrol.frame = CGRectMake(0
                                    , 10
                                    , frame.size.width
                                    , 15);
    
    if (_saveImageArray.count==0)
    {
        VerticalSettingLabel *labelTemp = nameLabelArray[0];
        if ([UserInfo shareUserInfo].isExhibitor)
        {
            labelTemp.frame = CGRectMake(0, 0, frame.size.width
                                         , frame.size.height);
        }
        else
        {
            labelTemp.frame = CGRectMake(0, 0, frame.size.width
                                         , 130);
        }
    }
    else
    {
        for (int i=0; i<_saveImageArray.count; i++)
        {
            UIImageView *temp = _saveImageArray[i];
            temp.frame = CGRectMake(i*frame.size.width
                                    , 0
                                    , frame.size.width
                                    , frame.size.height);
            
            if ([nameLabelArray[i] isKindOfClass:[VerticalSettingLabel class]])
            {
                VerticalSettingLabel *labelTemp = nameLabelArray[i];
                if ([UserInfo shareUserInfo].isExhibitor)
                {
                    labelTemp.frame = CGRectMake(0, 0, temp.frame.size.width
                                                 , temp.frame.size.height);
                }
                else
                {
                    labelTemp.frame = CGRectMake(0, 0, temp.frame.size.width
                                                 , 130);
                }
            }
        }
    }
    
    if (imgUrlArray.count==0)
    {
        _backgroundScrollImg.contentSize = CGSizeMake(frame.size.width
                                                      , frame.size.height);
    }
    else
    {
        _backgroundScrollImg.contentSize = CGSizeMake(frame.size.width*pageNumber, frame.size.height);
        [_backgroundScrollImg scrollRectToVisible:CGRectMake(frame.size.width, 0, frame.size.width, frame.size.height) animated:NO];//默认显示第一张
    }
}

-(void)setCellWithExhibitorName:(NSString *)name productImg:(NSArray*)pImgArray
{
    if (_saveImageArray==nil)
    {
        _saveImageArray = [NSMutableArray array];
    }
    else
    {
        for (UIView * temp in _saveImageArray)
        {
            [temp removeFromSuperview];
        }
        [_saveImageArray removeAllObjects];
    }
    
    imgUrlArray = pImgArray;
    imgNeedShowBigArray = [NSMutableArray array];
    nameLabelArray = [NSMutableArray array];
    
    if (pImgArray.count==0||[pImgArray[0] isEqualToString:@""])//没有图片或者图片为@""
    {
        UIImageView *firstBack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"顶部背景图"]];
        firstBack.contentMode = UIViewContentModeScaleAspectFill;
        firstBack.userInteractionEnabled = YES;
        firstBack.clipsToBounds = YES;
        [self.backgroundScrollImg addSubview:firstBack];
        [_saveImageArray addObject:firstBack];
        
        VerticalSettingLabel *nameShow = [[VerticalSettingLabel alloc] init];
        nameShow.textColor = [UIColor whiteColor];
        nameShow.font = [XXTool systemFontOfSize:17];
        nameShow.verticalAlignment = VerticalAlignmentMiddle;
        nameShow.textAlignment = NSTextAlignmentCenter;
        nameShow.userInteractionEnabled = NO;
        nameShow.numberOfLines = 0;
        nameShow.text = name;
        [firstBack addSubview:nameShow];
        [nameLabelArray addObject:nameShow];
    }
    else
    {
        for (int i=0; i<pImgArray.count; i++)
        {
            //首位放置图片用于循环滑动
            if (i==0)
            {
                UIImageView *imgTemp = [[UIImageView alloc] init];
                [imgTemp setImageWithUrl:pImgArray[pImgArray.count-1] placerHolder:[UIImage imageNamed:@"顶部背景图"]];
                imgTemp.contentMode = UIViewContentModeScaleAspectFill;
                imgTemp.userInteractionEnabled = YES;
                imgTemp.clipsToBounds = YES;
                imgTemp.tag = i-2;
                [self.backgroundScrollImg addSubview:imgTemp];
                [_saveImageArray addObject:imgTemp];
                [nameLabelArray addObject:imgTemp];
                
                UIImageView *firstBack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"顶部背景图"]];
                firstBack.contentMode = UIViewContentModeScaleAspectFill;
                firstBack.userInteractionEnabled = YES;
                firstBack.clipsToBounds = YES;
                firstBack.tag = i-1;
                [_backgroundScrollImg addSubview:firstBack];
                [_saveImageArray addObject:firstBack];
                
                VerticalSettingLabel *nameShow = [[VerticalSettingLabel alloc] init];
                nameShow.textColor = [UIColor whiteColor];
                nameShow.font = [XXTool systemFontOfSize:17];
                nameShow.verticalAlignment = VerticalAlignmentMiddle;
                nameShow.textAlignment = NSTextAlignmentCenter;
                nameShow.userInteractionEnabled = NO;
                nameShow.numberOfLines = 0;
                nameShow.text = name;
                [firstBack addSubview:nameShow];
                [nameLabelArray addObject:nameShow];
            }
            
            UIImageView *imgTemp = [[UIImageView alloc] init];
            [imgTemp setImageWithUrl:pImgArray[i] placerHolder:[UIImage imageNamed:@"顶部背景图"]];
            imgTemp.contentMode = UIViewContentModeScaleAspectFill;
            imgTemp.userInteractionEnabled = YES;
            imgTemp.clipsToBounds = YES;
            imgTemp.tag = i;
            [imgTemp addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickScrollImgShowFull:)]];
            [_backgroundScrollImg addSubview:imgTemp];
            [_saveImageArray addObject:imgTemp];
            [imgNeedShowBigArray addObject:imgTemp];
            [nameLabelArray addObject:imgTemp];
            
            if (i==pImgArray.count-1)
            {
                UIImageView *firstBack = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"顶部背景图"]];
                firstBack.contentMode = UIViewContentModeScaleAspectFill;
                firstBack.userInteractionEnabled = YES;
                firstBack.clipsToBounds = YES;
                firstBack.tag = i+1;
                [_backgroundScrollImg addSubview:firstBack];
                [_saveImageArray addObject:firstBack];
                
                VerticalSettingLabel *nameShow = [[VerticalSettingLabel alloc] init];
                nameShow.textColor = [UIColor whiteColor];
                nameShow.font = [XXTool systemFontOfSize:17];
                nameShow.verticalAlignment = VerticalAlignmentMiddle;
                nameShow.textAlignment = NSTextAlignmentCenter;
                nameShow.userInteractionEnabled = NO;
                nameShow.numberOfLines = 0;
                nameShow.text = name;
                [firstBack addSubview:nameShow];
                [nameLabelArray addObject:nameShow];
            }
        }
        self.pagecontrol.numberOfPages = pImgArray.count+1;
        _pagecontrol.currentPage = 0;
        pageNumber = _saveImageArray.count;
    }
    
    self.exhibitorName.text = name;
    
    [self refreshLayout:self.frame];
}

-(void)setCellWithCollectionOrOrderStatus:(XMLExhibitorCollectOrAppointment *)statusData
{
    if ([statusData.collection intValue]==1)
    {
        _collectButton.image = [UIImage imageNamed:@"收藏按钮_已点击"];
        _collectButton.text = NSLocalizedString(@"已收藏", nil);
    }
    else
    {
        _collectButton.image = [UIImage imageNamed:@"收藏按钮_未点击"];
        _collectButton.text = NSLocalizedString(@"收藏", nil);
    }
    
    if ([statusData.appointment intValue]==1)
    {
        _bespeakButton.image = [UIImage imageNamed:@"预约按钮_已点击"];
        _bespeakButton.text = NSLocalizedString(@"已预约", nil);
    }
    else
    {
        _bespeakButton.image = [UIImage imageNamed:@"预约按钮_未点击"];
        _bespeakButton.text = NSLocalizedString(@"预约", nil);
    }
    
}

#pragma mark scrollviewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    CGRect bounds = scrollView.frame;
    int nowPage = offset.x/bounds.size.width-1;
    if(nowPage==-1)
    {
        [_backgroundScrollImg scrollRectToVisible:CGRectMake((pageNumber-2)*self.frame.size.width, 0, self.frame.size.width, self.frame.size.height) animated:NO];
        [_pagecontrol setCurrentPage:pageNumber-3];
    }
    else if(nowPage==pageNumber-2)
    {
        [_backgroundScrollImg scrollRectToVisible:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height) animated:NO];
        [_pagecontrol setCurrentPage:0];
    }
    else
    {
        [_pagecontrol setCurrentPage:nowPage];
    }
    
}

-(void)clickBespeakButton
{
    if ([_delegate respondsToSelector:@selector(exhibitorsDetailsHeadCellDidClickAppointExhibitorButtonWithButton:)])
    {
        [_delegate exhibitorsDetailsHeadCellDidClickAppointExhibitorButtonWithButton:_bespeakButton];
    }
}

-(void)clickCollectButton
{
    if ([_delegate respondsToSelector:@selector(exhibitorsDetailsHeadCellDidClickCollectButtonWithButton:)])
    {
        [_delegate exhibitorsDetailsHeadCellDidClickCollectButtonWithButton:_collectButton];
    }
}

-(void)clickScrollImgShowFull:(UITapGestureRecognizer*)tapRecognizer
{
    NSInteger tag = [tapRecognizer view].tag;
    DKPhotoBrowser *dkPhotoB = [[DKPhotoBrowser alloc] init];
    dkPhotoB.delegate = self;
    [dkPhotoB setCurrentIndex:tag];
    [dkPhotoB show];
}

#pragma mark DKPhotoModelDelegate
-(DKPhotoModel*)photoBrowser:(DKPhotoBrowser *)browser imageUrlForIndex:(NSInteger)index
{
    DKPhotoModel *useType = [DKPhotoModel new];
    useType.url = imgUrlArray[index];
    useType.type = DKPhotoObjectTypePicture;
    useType.rectView = imgNeedShowBigArray[index];
    return useType;
}

-(NSInteger)numberOfPhotoBrowser:(DKPhotoBrowser *)browser
{
    return imgNeedShowBigArray.count;
}

@end
